package cn.koala.key.common.utils;

import cn.seed.common.utils.LoggerUtils;
import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description: 时间转换工具类
 * @author:XS
 * @time:18-8-14 下午5:09
 */
public class DateUtils {

    private static final String DAY = "yyyy-MM-dd";
    private static final String MINUTE = "yyyy-MM-dd HH:mm";
    private static final String SECOND = "yyyy-MM-dd HH:mm:ss";
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DAY);
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(SECOND);
    private static Logger logger = Logger.getLogger(DateUtils.class);

    private DateUtils() {

    }

    /**
     * @Description:将time 转换成时间戳  time格式  默认返回当前时间
     * @param: [time]
     * @return: long
     * @exception:
     * @author:　minhau
     * @time: 下午5:13 18-8-14
     */
    public static long convertDateToMillions(String time, String format) {

        long result = System.currentTimeMillis();
        try {
            result = choose(format).parse(time).getTime();
        } catch (ParseException e) {
            LoggerUtils.error(DateUtils.class, e.getMessage());
        }
        return result;
    }

    /**
     * @Description:默认将天转成时间戳
     * @param: [time]
     * @return: long
     * @exception:
     * @author:　minhau
     * @time: 下午5:39 18-8-14
     */
    public static long convertDateToMillions(String time) {

        long result = System.currentTimeMillis();
        try {
            result = choose(DAY).parse(time).getTime();
        } catch (ParseException e) {
            LoggerUtils.error(DateUtils.class, e.getMessage());
        }
        return result;
    }

    /**
     * @Description: 将时间戳转换到Date
     * @param: [millions]
     * @return: java.util.Date
     * @exception:
     * @author:　minhau
     * @time: 下午6:12 18-8-14
     */
    public static Date convertMillionsToDate(Long millions) {

        SimpleDateFormat day = new SimpleDateFormat(DAY);

        String temp = day.format(millions);
        Date result = null;
        try {
            result = day.parse(temp);
        } catch (ParseException e) {
            LoggerUtils.error(DateUtils.class, e.getMessage());
        }

        return result;
    }

    public static Date convertMillionsToSecond(Long millions) {

        SimpleDateFormat day = new SimpleDateFormat(SECOND);

        String temp = day.format(millions);
        Date result = null;
        try {
            result = day.parse(temp);
        } catch (ParseException e) {
            LoggerUtils.error(DateUtils.class, e.getMessage());
        }

        return result;
    }

    /**
     * @Description: 选择转换格式
     * @param: [format]
     * @return: java.text.SimpleDateFormat
     * @exception:
     * @author:　minhau
     * @time: 下午5:34 18-8-14
     */
    private static SimpleDateFormat choose(String format) {
        SimpleDateFormat day = new SimpleDateFormat(DAY);
        SimpleDateFormat minute = new SimpleDateFormat(MINUTE);
        SimpleDateFormat second = new SimpleDateFormat(SECOND);
        switch (format) {
            case DAY:
                return day;
            case MINUTE:
                return minute;
            case SECOND:
                return second;
            default:
                return day;
        }
    }

    /**
     * @Description:获取year
     * @param: [date]
     * @return: java.lang.String
     * @exception:
     * @author:　minhau
     * @time: 下午3:41 18-8-20
     */
    public static String getYear(Date date) {
        SimpleDateFormat year = new SimpleDateFormat("yyyy");
        return year.format(date);
    }

    /**
     * @Description:将Date转成　yyyy-MM-dd HH:mm:ss
     * @param: [date]
     * @return: java.lang.String
     * @exception:
     * @author:　minhau
     * @time: 下午4:16 18-8-21
     */
    public static String getSecond(Date date) {
        SimpleDateFormat second = new SimpleDateFormat(SECOND);
        return second.format(date);
    }

    /**
     * @Description:将时间转换成　yyyy-MM-dd
     * @param: [date]
     * @return: java.lang.String
     * @exception:
     * @author:　minhau
     * @time: 下午2:28 18-8-28
     */
    public static String getDay(Date date) {
        SimpleDateFormat day = new SimpleDateFormat(DAY);
        return day.format(date);
    }

    /**
     * @Description:将Date转成　yyyy-MM-dd HH:mm
     * @param: [date]
     * @return: java.lang.String
     * @exception:
     * @author:　minhau
     * @time: 下午4:16 18-8-21
     */
    public static String getMinute(Date date) {
        SimpleDateFormat second = new SimpleDateFormat(MINUTE);
        return second.format(date);
    }

    public static String format(Date date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static String format(Date date) {
        return format(date, SECOND);
    }

    public static Date parse(String dateString, String format) {
        if (dateString == null || "".equals(dateString)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            logger.error(e);
            return null;
        }
    }

    public static Date parse(String dateString) {
        return parse(dateString, SECOND);
    }

    public static long difference(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0;
        }
        if (date1.before(date2)) {
            return (date2.getTime() - date1.getTime()) / 1000;
        } else {
            return (date1.getTime() - date2.getTime()) / 1000;
        }
    }

    public static long difference(Date date) {
        if (new Date().before(date)) {
            return difference(new Date(), date);
        } else {
            return 0;
        }
    }

    public static Date localDate2Date(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static LocalDate date2LocalDate(Date date) {
        return date2LocalDateTime(date).toLocalDate();
    }

    public static LocalDate str2LocalDate(String dateString) {
        return LocalDate.parse(dateString, DATE_FORMATTER);
    }

    public static LocalDate str2LocalDate(String dateString, String format) {
        return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(format));
    }

    public static String localDate2Str(LocalDate localDate) {
        return localDate.toString();
    }

    public static String localDate2Str(LocalDate localDate, String format) {
        return DateTimeFormatter.ofPattern(format).format(localDate);
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static String localDateTime2Str(LocalDateTime localDateTime) {
        return DATE_TIME_FORMATTER.format(localDateTime);
    }

    public static String localDateTime2Str(LocalDateTime localDateTime, String format) {
        return DateTimeFormatter.ofPattern(format).format(localDateTime);
    }

    public static LocalDateTime str2LocalDateTime(String dateString) {
        return LocalDateTime.parse(dateString, DATE_TIME_FORMATTER);
    }

    public static LocalDateTime str2LocalDateTime(String dateString, String format) {
        return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(format));
    }

    public static long getDate(String dateStr, String format) {
        Date now = new Date();
        // 可以方便地修改日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            now = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            logger.error(e);
        }
        long time = now.getTime();
        return time;
    }
}
